package com.hc.likelistenmusic.presenter

import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.TextUtils
import android.util.Log
import com.google.gson.Gson
import com.hc.likelistenmusic.activity.OnlineMusicActivity
import com.hc.likelistenmusic.bean.OnlineMusicId
import com.hc.likelistenmusic.bean.OnlineMusicResponse
import com.hc.likelistenmusic.bean.OnlineMusicUrl
import com.hc.likelistenmusic.model.OnlineMusicModel
import com.hc.likelistenmusic.util.OkHttpUtil
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.listener.VideoAllCallBack
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager

class OnlineMusicPresenter(var mOnlineMusicActivity: OnlineMusicActivity) : VideoAllCallBack {
    val TAG = "HUACHEN_OnlineMusicPresenter"
    val TYPE_UPDATE_PROGRESS = 2
    lateinit var mSvp: StandardGSYVideoPlayer
    var mHandler: Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            if (msg.what == TYPE_UPDATE_PROGRESS) {
                mOnlineMusicActivity.onProgress(mSvp.currentPositionWhenPlaying.toInt(), mSvp.duration.toInt())
                sendProgress()
            }
        }
    }
    var mOnlineMusicModel:OnlineMusicModel
    init {
        mOnlineMusicModel = OnlineMusicModel(mOnlineMusicActivity.applicationContext)
    }

    fun getExitInfo(): String {
        return mOnlineMusicModel.getExitString()
    }

    fun requestOnlineMusicUrl(om: OnlineMusicResponse){
        prepareOnlineMusic("http://192.168.1.5:8088/music/selectMusicUrl/${om.name}/${om.singer}/${om.suffix}")
    }
    fun prepareOnlineMusic(url: String) {
        try {
            cancelProgress()
            mSvp.onVideoReset()
            mSvp.setUp(url,false,"")
            mSvp.startPlayLogic()
        }catch (e:Exception){
            mOnlineMusicActivity.prepareError()
            e.printStackTrace()
            Log.d(TAG, "缓冲失败1")
        }
    }

    fun seekTo(currentProgress: Int) {
        mSvp.seekTo(currentProgress.toLong())
    }
    fun sendProgress() {
        var m = Message.obtain()
        m.what = TYPE_UPDATE_PROGRESS
        mHandler.sendMessageDelayed(m, 1000)
    }

    fun cancelProgress() {
        mHandler.removeMessages(TYPE_UPDATE_PROGRESS)
    }

    fun onDestroy() {
        mSvp.setVideoAllCallBack(null)
        mHandler.removeCallbacksAndMessages(null)
        GSYVideoManager.releaseAllVideos()
    }

    fun MediaPlayOrPauseStatus(): Boolean {
        return mSvp.currentState == GSYVideoView.CURRENT_STATE_PLAYING
    }

    fun MediaPlay() {
        mSvp.onVideoResume()
        sendProgress()
    }

    fun MediaPause() {
        mSvp.onVideoPause()
        cancelProgress()
    }

    fun setVideoPlayer(svp: StandardGSYVideoPlayer) {
        mSvp = svp
        mSvp.setVideoAllCallBack(this)
        PlayerFactory.setPlayManager(Exo2PlayerManager::class.java)
    }

    override fun onStartPrepared(url: String?, vararg objects: Any?) {

    }

    override fun onPrepared(url: String?, vararg objects: Any?) {
        try {
            mSvp.onVideoResume()
            mOnlineMusicActivity.onProgress(0, mSvp.duration.toInt())
            mOnlineMusicActivity.onMediaPlayerPrepared()
            sendProgress()
        }catch (e:Exception){
            CoroutineScope(Dispatchers.Main).launch {
                mOnlineMusicActivity.prepareError()
            }
            e.printStackTrace()
            Log.d(TAG, "缓冲失败2")
        }
    }

    override fun onClickStartIcon(url: String?, vararg objects: Any?) {

    }

    override fun onClickStartError(url: String?, vararg objects: Any?) {

    }

    override fun onClickStop(url: String?, vararg objects: Any?) {

    }

    override fun onClickStopFullscreen(url: String?, vararg objects: Any?) {

    }

    override fun onClickResume(url: String?, vararg objects: Any?) {

    }

    override fun onClickResumeFullscreen(url: String?, vararg objects: Any?) {

    }

    override fun onClickSeekbar(url: String?, vararg objects: Any?) {

    }

    override fun onClickSeekbarFullscreen(url: String?, vararg objects: Any?) {

    }

    override fun onAutoComplete(url: String?, vararg objects: Any?) {
        mOnlineMusicActivity.onPlayCompletion()
    }

    override fun onComplete(url: String?, vararg objects: Any?) {

    }

    override fun onEnterFullscreen(url: String?, vararg objects: Any?) {

    }

    override fun onQuitFullscreen(url: String?, vararg objects: Any?) {

    }

    override fun onQuitSmallWidget(url: String?, vararg objects: Any?) {

    }

    override fun onEnterSmallWidget(url: String?, vararg objects: Any?) {

    }

    override fun onTouchScreenSeekVolume(url: String?, vararg objects: Any?) {

    }

    override fun onTouchScreenSeekPosition(url: String?, vararg objects: Any?) {

    }

    override fun onTouchScreenSeekLight(url: String?, vararg objects: Any?) {

    }

    override fun onPlayError(url: String?, vararg objects: Any?) {

    }

    override fun onClickStartThumb(url: String?, vararg objects: Any?) {

    }

    override fun onClickBlank(url: String?, vararg objects: Any?) {

    }

    override fun onClickBlankFullscreen(url: String?, vararg objects: Any?) {

    }
}